package br.com.financeiro;

import java.text.SimpleDateFormat;
import java.util.Date;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.app.Dialog;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.EditText;
import android.widget.Spinner;


public class BuscarContaPeridoActivity extends Activity {
	
	private String[] filtroContas = new String[] { "Despesa",  "Receita","Fluxo de Caixa"};
	private EditText campoDataFinal, campoDataInicial;
	private DatePicker datePickerDialog;
	private Button buscarPeriodo;
	private Spinner filtroConta;
	private Date dataAtual;
	@SuppressLint("SimpleDateFormat")
	SimpleDateFormat formato = new SimpleDateFormat("dd/MM/yyyy");
	
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_buscar_conta_periodo);
		preencherSpinnerFiltroConta();
		
		dataAtual = new Date();
		
		campoDataInicial = (EditText) findViewById(R.id.campoDataInicial);
		campoDataInicial.setText(formato.format(dataAtual));
		campoDataInicial.setInputType(0);
		campoDataInicial.setOnClickListener(campoDataInicialOnClickListener);

		campoDataFinal = (EditText) findViewById(R.id.campoDataFinal);
		campoDataFinal.setText(formato.format(dataAtual));
		campoDataFinal.setInputType(0);
		campoDataFinal.setOnClickListener(campoDataFinalOnClickListener);
		
		
		filtroConta = (Spinner) findViewById(R.id.filtroConta);
		buscarPeriodo = (Button) findViewById(R.id.btBuscarPeriodo);
		buscarPeriodo.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View arg0) {
				// TODO Auto-generated method stub
			String 	dataInicial = campoDataInicial.getText().toString();
			String dataFinal = campoDataFinal.getText().toString();
			String filtro = filtroConta.getSelectedItem().toString();
			Intent listaIntent = new Intent(BuscarContaPeridoActivity.this, ListaContaPeriodoActivity.class);
			listaIntent.putExtra("dataInicial", dataInicial);
			listaIntent.putExtra("dataFinal", dataFinal);
			listaIntent.putExtra("filtro", filtro);
			startActivity(listaIntent);
			}
		});
		
	}
	
	private OnClickListener campoDataInicialOnClickListener = new OnClickListener() {
		@Override
		public void onClick(View v) {
			showDataDialog(1);
		}
	};

	private OnClickListener campoDataFinalOnClickListener = new OnClickListener() {
		@Override
		public void onClick(View v) {
			showDataDialog(2);
		}
	};

	private void showDataDialog(final int idCampo) {
		final Dialog dialog = new Dialog(this);
		dialog.setContentView(R.layout.data_dialog);
		dialog.setTitle("Informe a data");

		datePickerDialog = (DatePicker) dialog.findViewById(R.id.dateDialog);

		final Button btDialogOk = (Button) dialog.findViewById(R.id.btDialogOk);
		btDialogOk.setOnClickListener(new View.OnClickListener() {
			public void onClick(View v) {
				String dataFomato = datePickerDialog.getDayOfMonth() + "/"
						+ (datePickerDialog.getMonth() + 1) + "/"
						+ datePickerDialog.getYear();

				if (idCampo == 1) {
					campoDataInicial.setText(dataFomato);
				} else {
					campoDataFinal.setText(dataFomato);
				}
				dialog.dismiss();
			}
		});

		dialog.show();
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.listagem, menu);
		return true;
	}
	
	private void preencherSpinnerFiltroConta() {
		final Spinner combo = (Spinner) findViewById(R.id.filtroConta);
		
		ArrayAdapter<String> adaptador = new ArrayAdapter<String>(this,
				android.R.layout.simple_spinner_item, filtroContas);
		
		adaptador.setDropDownViewResource(android.R.layout.simple_spinner_item);
		combo.setAdapter(adaptador);
	}

}